#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
#define ls (rt<<1)
#define rs (rt<<1|1)
int n, m;
int a[maxn];
void down(int rt) {
  int mn = rt;
  if(ls<=n && a[ls]<a[mn]) mn = ls;
  if(rs<=n && a[rs]<a[mn]) mn = rs;
  if(mn!=rt) {
    swap(a[mn], a[rt]);
    down(mn);
  }
}
int main() {
  cin>>n>>m; for(int i=1;i<=n; ++i)cin>>a[i];
  for(int i=n/2; i; i--) {
    down(i);
  }
  while(m--) {
    cout<<a[1]<<' ';
    a[1] = a[n--];
    down(1);
  }
  cout<<'\n';
}
